package cn.zn.modules.budget.cur.controller;

import cn.zn.framework.common.utils.PageUtils;
import cn.zn.framework.common.utils.Query;
import cn.zn.framework.common.utils.R;
import cn.zn.modules.budget.cur.entity.TBudgetInfoDetailEntity;
import cn.zn.modules.budget.cur.service.TBudgetInfoDetailService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * @author zn
 * @email zhunahao@qq.com
 * @date 2019-12-10 14:27:52
 */
@RestController
@RequestMapping("budget/tbudgetinfodetail")
public class TBudgetInfoDetailController {

    @Autowired
    private TBudgetInfoDetailService tBudgetInfoDetailService;

    /**
     * 上传预算导入文件
     *
     * @param file
     * @return
     */
    @RequestMapping("/importDetail")
    public R importDetail(@RequestParam("file") MultipartFile file) {
        tBudgetInfoDetailService.importDetail(file);
        return R.ok();
    }

    /**
     * 获取未匹配的部门
     *
     * @return
     */
    @RequestMapping("/noDepts")
    public R noDepts() {
        List<String> list = tBudgetInfoDetailService.queryNoDept();
        return R.ok().put("data", list);
    }

    /**
     * 获取未匹配的项目
     *
     * @return
     */
    @RequestMapping("/noProjects")
    public R noProjects() {
        List<String> list = tBudgetInfoDetailService.queryNoProject();
        return R.ok().put("data", list);
    }

    /**
     * 保存更改部门信息
     *
     * @param deptOld
     * @param dept
     * @return
     */
    @RequestMapping("/saveDept")
    public R saveDept(String deptOld, String dept) {
        tBudgetInfoDetailService.saveDept(deptOld, dept);
        return R.ok();
    }

    @RequestMapping("/saveProjectAll")
    public R saveProjectAll() {
        tBudgetInfoDetailService.saveProjectAll();
        return R.ok();
    }

    @RequestMapping("/saveAll")
    public R saveAll(String remark) {
        tBudgetInfoDetailService.saveAll(remark);
        return R.ok();
    }

    /**
     * 列表
     */
    @PostMapping("/list")
    @RequiresPermissions("budget:tbudgetinfodetail:list")
    public R list(@RequestBody Map<String, Object> params) {
        if (!params.containsKey("deptCode") && !params.containsKey("checkdept")) {
            return R.ok().put("page", new PageUtils(new Query<TBudgetInfoDetailEntity>().getPage(params)));
        }
        PageUtils pageUtils = tBudgetInfoDetailService.queryPage(params);

        return R.ok().put("page", pageUtils);
    }

    /**
     * 查询待审核
     *
     * @param params
     * @return
     */
    @RequestMapping("/toAudit")
    @RequiresPermissions("budget:tbudgetinfodetail:list")
    public R toAudit(@RequestParam Map<String, Object> params) {
        params.put("state", 0);
        PageUtils pageUtils = tBudgetInfoDetailService.queryPage(params);

        return R.ok().put("page", pageUtils);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("budget:tbudgetinfodetail:info")
    public R info(@PathVariable("id") String id) {
        TBudgetInfoDetailEntity tBudgetInfoDetail = tBudgetInfoDetailService.findDetail(id);

        return R.ok().put("info", tBudgetInfoDetail);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("budget:tbudgetinfodetail:save")
    public R save(@RequestBody TBudgetInfoDetailEntity tBudgetInfoDetail) {
        tBudgetInfoDetail.setYear(LocalDate.now().getYear());
        tBudgetInfoDetailService.save(tBudgetInfoDetail);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("budget:tbudgetinfodetail:update")
    public R update(@RequestBody TBudgetInfoDetailEntity tBudgetInfoDetail) {
        TBudgetInfoDetailEntity tBudgetInfoDetailOld = tBudgetInfoDetailService.getById(tBudgetInfoDetail.getId());
        if (tBudgetInfoDetailOld.getState() == 0) {
            tBudgetInfoDetailService.updateById(tBudgetInfoDetail);
        } else {
            return R.error("预算已审核，不能修改");
        }
        return R.ok();
    }

    /**
     * 审核通过
     *
     * @param ids
     * @return
     */
    @PutMapping("/audit/agree")
    @RequiresPermissions("budget:tbudgetinfodetail:update")
    public R updateAuditAgree(@RequestBody String[] ids) {
        tBudgetInfoDetailService.saveAudit(Arrays.asList(ids), true);

        return R.ok();
    }

    /**
     * 审核不通过
     *
     * @param ids
     * @return
     */
    @PutMapping("/audit/notagree")
    @RequiresPermissions("budget:tbudgetinfodetail:update")
    public R updateAuditNotAgree(@RequestBody String[] ids) {

        tBudgetInfoDetailService.saveAudit(Arrays.asList(ids), false);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("budget:tbudgetinfodetail:delete")
    public R delete(@RequestBody String[] ids) {
        tBudgetInfoDetailService.deleteEntity(Arrays.asList(ids));

        return R.ok();
    }

}
